<?php
namespace app\index\controller;
use think\Controller;
use think\Session;
use think\Request;
use think\View;
use think\Db;
use think\Model;
//产品sku
class Productssku extends Common
{
	public function huishou($page=1,$list=10){
		$where="sk.is_deleted=0 and sk.is_actived=0 and s.is_deleted=0 and s.is_actived=1 and c.is_deleted=0 and c.is_actived=1 and b.is_actived=1 and b.is_deleted=0 ";
		$sum=Db::table("pro_sku")->alias("sk")
    	->join("pro_series s","sk.series_id=s.id")
    	->join("pro_category c","s.category_id=c.id")
    	->join("pro_brands b","c.brand_id=b.id")->where($where)->count();
    	$maxpage=ceil($sum/$list);
        $page=$page<$maxpage?$page:$maxpage;
        $page=$page>1?$page:1;

        $start=($page-1)*$list;

    	$dataps=Db::table("pro_sku")->alias("sk")
    	->join("pro_series s","sk.series_id=s.id")
    	->join("pro_category c","s.category_id=c.id")
    	->join("pro_brands b","c.brand_id=b.id")
        ->join("users uc","sk.c_users_id=uc.id","LEFT")
        ->join("employee ec","uc.employee_id=ec.id","LEFT")
        ->join("users um","sk.m_users_id=um.id","LEFT")
        ->join("employee em","um.employee_id=em.id","LEFT")
        ->where($where)
        ->order("sk.id")
        ->limit($start,$list)
        ->field("sk.id,b.brand_name,c.category_name,s.series_name,sk.sku_no,sk.sku_name,ec.em_name as cname,sk.created_on,em.em_name as mname,sk.modifiled_on")
        ->select();

        for($i=0;$i<count($dataps);$i++){
        	$dataps[$i]["created_on"]=date("Y-m-d H:i",strtotime($dataps[$i]["created_on"]));
        	if($dataps[$i]["modifiled_on"])
        		$dataps[$i]["modifiled_on"]=date("Y-m-d H:i",strtotime($dataps[$i]["modifiled_on"]));
        	
        }
    	
    	$this->assign("dataps",$dataps);
		$this->assign("sum",$sum);
		$this->assign("page",$page);
		$this->assign("list",$list);
		$this->assign("maxpage",$maxpage);
		return $this->fetch('huishou');

	}
	public function sku($page=1,$list=10){
		//接收数据
    	$request = Request::instance();

    	
    	$data=$request->post();
    	$where="sk.is_deleted=0 and sk.is_actived=1 and s.is_deleted=0 and s.is_actived=1 and c.is_deleted=0 and c.is_actived=1 and b.is_actived=1 and b.is_deleted=0 ";
    	if($data){
    		
    		if(trim($data["series_name"]))
    			$where.="and s.series_name like '%{$data["series_name"]}%' ";
    		if(trim($data["category_name"]))
    			$where.="and c.category_name like '%{$data["category_name"]}%' ";
    		if(trim($data["brand_name"]))
    			$where.="and b.brand_name like '%{$data["brand_name"]}%' ";
    		if(trim($data["sku_name"]))
    			$where.="and sk.sku_name like '%{$data["sku_name"]}%' ";
    		if(trim($data["sku_no"]))
    			$where.="and sk.sku_no like '%{$data["sku_no"]}%' ";


    	}else{
    		$data=["series_name"=>"","category_name"=>"","brand_name"=>"","sku_no"=>"","sku_name"=>""];
    	}
    	$sum=Db::table("pro_sku")->alias("sk")
    	->join("pro_series s","sk.series_id=s.id")
    	->join("pro_category c","s.category_id=c.id")
    	->join("pro_brands b","c.brand_id=b.id")->where($where)->count();
    	$maxpage=ceil($sum/$list);
        $page=$page<$maxpage?$page:$maxpage;
        $page=$page>1?$page:1;

        $start=($page-1)*$list;

    	$dataps=Db::table("pro_sku")->alias("sk")
    	->join("pro_series s","sk.series_id=s.id")
    	->join("pro_category c","s.category_id=c.id")
    	->join("pro_brands b","c.brand_id=b.id")
        ->join("users uc","sk.c_users_id=uc.id","LEFT")
        ->join("employee ec","uc.employee_id=ec.id","LEFT")
        ->join("users um","sk.m_users_id=um.id","LEFT")
        ->join("employee em","um.employee_id=em.id","LEFT")
        ->where($where)
        ->order("sk.id")
        ->limit($start,$list)
        ->field("sk.id,b.brand_name,c.category_name,s.series_name,sk.sku_no,sk.sku_name,ec.em_name as cname,sk.created_on,em.em_name as mname,sk.modifiled_on")
        ->select();

        for($i=0;$i<count($dataps);$i++){
        	$dataps[$i]["created_on"]=date("Y-m-d H:i",strtotime($dataps[$i]["created_on"]));
        	if($dataps[$i]["modifiled_on"])
        		$dataps[$i]["modifiled_on"]=date("Y-m-d H:i",strtotime($dataps[$i]["modifiled_on"]));
        	
        }
    	$this->assign("data",$data);
    	$this->assign("dataps",$dataps);
		$this->assign("sum",$sum);
		$this->assign("page",$page);
		$this->assign("list",$list);
		$this->assign("maxpage",$maxpage);
		return $this->fetch('sku');
	}
	//新增
	public function new_sku(){
		$request = Request::instance();
        
        $datas=$request->post();//接收数据
       // print_r($data);exit;
        if(!$datas){
        	return 0;
        }
        //id:$("#isupdate").val(),sku_name:$("#sku_name").val(),ser_id:$("#ser_id").val(),sku_no:$("#sku_no").val()
        $id=$datas["id"];$sku_name=trim(htmlspecialchars($datas["sku_name"]));$series_id=trim($datas["ser_id"]);
        $sku_no=trim(htmlspecialchars($datas["sku_no"]));
        if(!$sku_name)
        	return 2;//系列名空
        if(!$series_id)
        	return 3;//品类空
		if(!$sku_no)
        	return 6;//号码空
        if($id){
        	//修改
        	$iscunzai=Db::table("pro_sku")->where("sku_name='{$sku_name}' and is_deleted=0 and id<>$id")->count();
			if($iscunzai)
				return 4;//重名
			$data["id"]=$id;
			$data["sku_name"]=$sku_name;
			$data["series_id"]=$series_id;
			$data["m_users_id"]=Session::get("login_id");
			$data["modifiled_on"]=date("Y-m-d H:i:s");
			Db::startTrans(); //开启事物
			 try{
                $res1=Db::table("pro_sku")->update($data);
             }catch(\Exception $e){
                  Db::rollBack();
                  return 5;//失败
             }
             if($res1){
             	Db::commit();
             	return 1;
             }else{
             	Db::rollBack();
                  return 5;//失败
             }
        }else{
        	$iscunzai=Db::table("pro_sku")->where("sku_name='{$sku_name}'  and is_deleted=0")->count();
			if($iscunzai)
				return 4;//重名
			$iscunzain=Db::table("pro_sku")->where("sku_no='{$sku_no}'  and is_deleted=0")->count();
			if($iscunzain)
				return 7;//重号
        	//创建
        	$data["sku_name"]=$sku_name;
        	$data["sku_no"]=$sku_no;
        	$data["c_users_id"]=Session::get("login_id");
        	$data["created_on"]=date("Y-m-d H:i:s");
        	$data["series_id"]=$series_id;

        	Db::startTrans(); //开启事物
			 try{
                $res1=Db::table("pro_sku")->insert($data);
             }catch(\Exception $e){
                  Db::rollBack();
                  return 5;//失败
             }
             if($res1){
             	Db::commit();
             	return 1;
             }else{
             	Db::rollBack();
                  return 5;//失败
             }

        }
	}
	//获取sku
	public function getsku(){
		$request = Request::instance();
        
        $data=$request->post();//接收数据
        //$data["bid"]=2;
		$where=["sk.id"=>$data["skid"]];
		$arr=Db::table("pro_sku")->alias("sk")
		->join("pro_series s","sk.series_id=s.id")
		->where($where)->field("sk.id,sk.sku_no,sk.sku_name,s.id as sid,s.series_name")->find();
		return json_encode($arr);
	}
	//删除系列
	public function dropsku(){
		$request = Request::instance();
        
        $data=$request->post();//接收数据
        //print_r($data);exit;
        //$data["m_users_id"]=Session::get("login_id");
			//$data["modifiled_on"]=date("Y-m-d H:i:s");
		$where=["id"=>$data["skid"],"is_actived"=>0,"m_users_id"=>Session::get("login_id"),"modifiled_on"=>date("Y-m-d H:i:s")];
		$res=Db::table("pro_sku")->update($where);
		if($res)
			return 1;
		else
			return 2;
	}
	//恢复系列
	public function huifusku(){
		$request = Request::instance();
        
        $data=$request->post();//接收数据
        //print_r($data);exit;
        //$data["m_users_id"]=Session::get("login_id");
			//$data["modifiled_on"]=date("Y-m-d H:i:s");
		$where=["id"=>$data["skid"],"is_actived"=>1,"m_users_id"=>Session::get("login_id"),"modifiled_on"=>date("Y-m-d H:i:s")];
		$res=Db::table("pro_sku")->update($where);
		if($res)
			return 1;
		else
			return 2;
	}
	//获得sku树
	public function getskulist(){
		 $request = Request::instance();
        
        $data=$request->post();//接收数据
        //print_r($data);exit;
        //$data["cid"]=2;
        $result=[];
        $where=["is_actived"=>1,"is_deleted"=>0];
        $res=Db::table("pro_brands")->where($where)->select();
         for($i=0;$i<count($res);$i++){
                $result[$i]["id"]="B".$res[$i]["id"];
               
                $result[$i]["name"]=$res[$i]["brand_name"];
                $result[$i]["pId"]=0;
                $result[$i]["nocheck"]=true;
                
           }
           $res2=Db::table("pro_category")->where($where)->select();
            for($i=0;$i<count($res2);$i++){
             
                $result[$i+count($res)]["id"]="C".$res2[$i]["id"];
               
                 $result[$i+count($res)]["name"]=$res2[$i]["category_name"];
                $result[$i+count($res)]["pId"]="B".$res2[$i]["brand_id"];
                $result[$i+count($res)]["nocheck"]=true;
                
           }
           $res3=Db::table("pro_series")->where($where)->select();
           for($i=0;$i<count($res3);$i++){
             
                $result[$i+count($res)+count($res2)]["id"]="S".$res3[$i]["id"];
               
                 $result[$i+count($res)+count($res2)]["name"]=$res3[$i]["series_name"];
                $result[$i+count($res)+count($res2)]["pId"]="C".$res3[$i]["category_id"];
               
                 $result[$i+count($res)+count($res2)]["nocheck"]=true;
           }
           $res4=Db::table("pro_sku")->where($where)->select();
           for($i=0;$i<count($res4);$i++){
             
                $result[$i+count($res)+count($res2)+count($res3)]["id"]=$res4[$i]["id"];
               
                 $result[$i+count($res)+count($res2)+count($res3)]["name"]=$res4[$i]["sku_name"];
                $result[$i+count($res)+count($res2)+count($res3)]["pId"]="S".$res4[$i]["series_id"];
               if($data["skuid"]==$res4[$i]["id"]){
               	$result[$i+count($res)+count($res2)+count($res3)]["checked"]=true;
               }
                
           }
           return json_encode($result);
	}
}